import Taro from '@tarojs/taro';

import {
  Command,
  GAME_STORAGE_INFO,
  GAME_RPICE_TYPE,
  OSS_BASE_PATH_PREFIX,
  PRODUCT_STATUS
} from '../constant';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async status() {
      // 或取当前状态值
      let {
        main: {isLoading},
      } = getData();
      action.commonChange('main.isLoading', !isLoading);
    },

    /**
     * 查询用户信息
     */
    async loadUserInfo(payload) {
      console.log('action.ts loadUserInfo payload', payload);

      // action.commonChange('main.userInfo', {});
      action.commonChange('main.response.loadUser', '');
      action.commonChange('main.status.loadingUser', true);

      let {data, code, message} = await api.customerBaseController.findCustomerBaseInfoForGame({});
      let userInfo: any = data || null;
      if (!userInfo) {
        userInfo = Taro.getStorageSync(GAME_STORAGE_INFO);
      }
      let accountType = '';
      if (userInfo && userInfo.customerName) {
        // 判断是否是会员
        if (!userInfo.isAquamarineMember && !userInfo.isCrystalMember) {
          accountType = '';
        } else if (userInfo.isAquamarineMember && userInfo.isCrystalMember) {
          accountType = 'isAquamarineMember&isCrystalMember';
        } else {
          accountType = userInfo.isAquamarineMember ? 'isAquamarineMember' : 'isCrystalMember';
        }
      }
      userInfo = {
        ...userInfo,
        accountType,
      };

      action.updateUserInfo(userInfo);

      action.commonChange('main.userInfo', {
        ...userInfo,
      });
      action.commonChange('main.response.loadUser', code);
      action.commonChange('main.status.loadingUser', false);
    },

    /**
     * 更新用户信息(storage 缓存)
     */
    async updateUserInfo(userInfo) {
      let info = Taro.getStorageSync(GAME_STORAGE_INFO) || {};
      info = {
        ...info,
        ...userInfo,
      };
      Taro.setStorageSync(GAME_STORAGE_INFO, info);
    },

    /**
     * 查询兑换列表
     */
    async loadExchangeItems(payload) {
      action.commonChange('main.exchangeItems', []);
      action.commonChange('main.response.loadExchange', '');
      action.commonChange('main.status.loadingExchange', true);

      //编辑参数
      let params = {
        // ...payload
      };

      let {data, code, message} = await api.GoldCoinsController.getGamePrizeList(params);
      data = data || [];

      let page = {
        ...payload,
        total: data.length || 0, // 未分页暂时这么弄
      };

      // 不同类型的基本信息
      let result = [];
      let infoState = {
        1: 'couponInfoVO', // 优惠券信息
        3: 'propVO', // 道具
        0: 'goodsInfoVO',
      };

      data.map((i) => {
        let infoVoState = infoState[i.gamePrizeConfig.prizeType];
        result.push({
          ...i.gamePrizeConfig,
          img: i.gamePrizeConfig.img,
          endTime: i[infoVoState] ? i[infoVoState].endTime : '', // 道具、优惠券有endTime,
          name: !i.gamePrizeConfig.name && (i.gamePrizeConfig.prizeType == GAME_RPICE_TYPE['gold']) ? '魔力币' : i.gamePrizeConfig.name,
          status: i.gamePrizeConfig.remainingNum == 0 && (i.gamePrizeConfig.prizeType == GAME_RPICE_TYPE['product']) ? PRODUCT_STATUS["withdraw"] : PRODUCT_STATUS["pushed"] // 只有商品有下架概念
        });
      });
      console.log('action.ts loadExchangeItems result', result);

      let {
        main: {exchangeItems},
      } = getData();

      if (page.pageNum > 0) {
        result = exchangeItems.concat(data.content);
      }
      action.commonChange('main.exchangeItems', result);
      action.commonChange('main.page', {...page});
      action.commonChange('main.response.loadExchange', code);
      action.commonChange('main.status.loadingExchange', false);
    },

    /**
     * 兑换内容
     */
    async exchangeItem(payload) {
      console.log('action.ts exchangeItem payload', payload);

      action.commonChange('main.response.exchange', '');
      action.commonChange('main.exchangeItem', {});
      action.commonChange('main.status.exchanging', true);

      // 编辑参数
      let params = {
        id: payload.target.prizeId
      };

      let {data, code, message} = await api.GoldCoinsController.exchangePrize(params);

      action.commonChange('main.codeMessage', message);
      action.commonChange('main.exchangeItem', payload.target);
      action.commonChange('main.response.exchange', code);
      action.commonChange('main.status.exchanging', false);
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageGoldCoinsExchangePaMain'),
  };
}
